// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/equipment/api/AssetManageApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/equipment/model/AssetListQueryReq'
import OneClass from '@api/equipment/model/AssetReq'
import AddClass from '@api/equipment/model/AssetInsertReq'
import EditClass from '@api/equipment/model/AssetUpdateReq'
import SubmitClass from '@api/equipment/model/AssetOperateReq'
import UploadClass from '@api/equipment/model/AssetImportByExcelReq'
import BaseClass from '@api/equipment/model/AssetDto'
import FileClass from '@api/equipment/model/AssetFileDto'
import PropertyClass from '@api/equipment/model/AssetPropertyDto'
import TagClass from '@api/base/model/DeviceLabelDto'

const DelClass = OneClass
const DownloadClass = OneClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const FILE = [FileClass, 'assetFileList', 1]
const PROPERTY = [PropertyClass, 'assetPropertyDto', 1]
const TAG = [TagClass, 'assetLabelDtos', 1]
const BASE = [BaseClass, 'assetInfo', '', (dto, obj) => CORE.grpcDTO(dto, obj, FILE, PROPERTY, TAG)]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  validParam(param) {
    return !CORE.isEmpty(param.statusList)
  },
  async getList(param, TIP = `查询${CORE.MAP.ASSET}列表`) {
    const DTO = CORE.grpcDTO(ListClass, param, COM, [BaseClass, 'assetInfo'], TAG)
    DTO.setStatus(param.statusList)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryAssetList', DTO)
    return [resp.comQuery.totalCount, resp.assetInfoList || [], resp.map || {}]
  },
  dealList([obj], { state: { MAP } }) {
    if (MAP.COUNT) MAP.COUNT = obj || {}
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = `查询${CORE.MAP.ASSET}详情`) {
    const DTO = CORE.grpcDTO(OneClass, { ids: [id] })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryAssetDetail', DTO)
    return resp.assetInfo || {}
  },
  transOne(obj) {
    obj.tagIdList = (obj.assetLabelDtos || []).map(({ labelId }) => labelId)
    return obj
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  transAdd(one) {
    const assetCode = one.assetCode === '自动生成' ? '' : one.assetCode
    return { ...one, assetCode }
  },
  async add(one, TIP = `新增${CORE.MAP.ASSET}`) {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'insertAsset', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = `修改${CORE.MAP.ASSET}`) {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'updateAsset', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = `删除${CORE.MAP.ASSET}`) {
    const DTO = CORE.grpcDTO(DelClass, { ids: [id] })
    await CORE.grpc(TIP, CLIENT, 'deleteAsset', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 提交 —————————————————————————————— //

  async submit(param, TIP = '操作') {
    const DTO = CORE.grpcDTO(SubmitClass, param)
    await CORE.grpc(TIP, CLIENT, 'operateAssetList', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 上传 —————————————————————————————— //

  async upload(param, TIP = '导入Excel模板') {
    const DTO = CORE.grpcDTO(UploadClass, param)
    await CORE.grpc(TIP, CLIENT, 'importAssetByExcel', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 下载 —————————————————————————————— //

  async download(param, TIP = '下载模板') {
    const DTO = CORE.grpcDTO(DownloadClass, param)
    await CORE.grpcDownload(TIP, CLIENT, 'downloadAssetImportModel', DTO, null, '设备导入模板.xlsx')
    return CORE.grpcOK(TIP)
  },
}
